﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using TERP.WaterClient.DbServices;
using TERP.WaterClient.Entities.Booking;
using System.Data;
using TERP.WaterClient.Entities.Auth;
using TERP.WaterClient.Common;


namespace TERP.WaterClient.Common
{
    public class MasterInit
    {
        private static MasterInit _instance;

		/// <summary>
		/// 
		/// </summary>
        public static MasterInit Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new MasterInit();
                return _instance;
            }
        }
        /////////////////////////////////
        private ILoadingListener _listener;

        public void SetListener(ILoadingListener listener)
        {
            this._listener = listener;
        }

		#region DECLARATIONS
       

		private FunctionCollection _lstFunction;

        

		#endregion

		public MasterInit(){
			_lstFunction = new FunctionCollection();            
		}        
        
		public FunctionCollection ListFunction
		{
			get { return _lstFunction; }
		}      


        void Init()
        {
                 
        }
        public void StartLoading()
        {
            Init();
			try
			{
                AuthService authSv = new AuthService();
                //
                _listener.Loading_Progress(10);
                SystemGlobal.Instance.ServerDateTime = authSv.GetServerDateTime();
                UserService _userService = new UserService();
                SystemGlobal.Instance.user = _userService.GetCurrentUser();

                _listener.Loading_Progress(30);

                #region //Init DB Master
				////Country
				//CountryService countrSv = new CountryService();
				//_dtCountry = countrSv.Its_Country_Get("", "").Tables[DbTable.Tbl_Country];

				////City
				//CityService CitySv = new CityService();
				//_dtCity = CitySv.Its_City_Get("", "","","").Tables[DbTable.Tbl_CountryCity];
				//_listener.Loading_Progress(50);

                #endregion

				new PriceService().GetCurrentPrice();

                //User
				_lstFunction = new UserService().GetFunctionForUser(
					 SystemGlobal.Instance.Username,
					 SystemConfig.Instance.PartnerCode);
				//
                _listener.Loading_Progress(90);             
                Thread.Sleep(100);
                ////
                _listener.Loading_Completed();
			}
			catch (Exception ex)
			{
				throw ex;
			}
        }
    }
}
